package com.wushijia.java8.lambda;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Stream;

/**
 * description:入门lambda体验:过滤不为0的整数数组，打印
 *
 * @author yang
 * @date 2019/2/15 16:45
 */
public class Lambda0 {

  public static void main(String[] args) {
    Integer[] arr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
    Integer[] arr1 = ql(arr);
    System.out.print("以前写法：");
    for (int i = 0; i < arr1.length; i++) {
      System.out.print(arr1[i]);
    }
    System.out.println();
    System.out.print("Lambda表达式写法：");
    new ArrayList<>(Arrays.asList(arr)).stream().filter(i -> !i.equals(0)).collect(toList()).forEach(System.out::print);
    System.out.println("");
    Stream.of(arr).filter(i -> !i.equals(0)).collect(toList()).forEach(System.out::print);
  }

  private static Integer[] ql(Integer[] arr) {
    int sum = 0;
    for (int j = 0; j < arr.length; j++) {
      if (arr[j] == 0) {
        sum++;
      }
    }
    Integer[] arr1 = new Integer[arr.length - sum];
    int k = 0;
    for (int i = 0; i < arr.length; i++) {
      if (arr[i] != 0) {
        arr1[k++] = arr[i];
      }
    }
    return arr1;
  }
}
